package it.free.maya.worddistinct;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Entry point
 * @author Stefano Falconetti
 */
public class Main {

	private static Logger logger = Logger.getLogger(Main.class);
	
	/**
	 * @param args are the configuration parameters, one in this case, the config file path
	 */
	public static void main(String[] args) {
		
		if( (args == null) || 
			(args.length != 1) ){
		   logger.error("Wrong number of arguments, pass config file path only");
		   System.exit(-1);
		}
		
		String driver = null;
		String connectionUrl = null;
		String username = null;
		String password = null;
		String query = null;
		int queryTimeoutSeconds = 0;
		
	    String configFilePath = null;
	    String stopWordsFilePath = null;
	    String synonymsFilePath = null;
	    String saveReportFileName = null;
	    boolean enableSynonyms = false;
	    boolean enableStopwords = false;
	    
	    Properties properties = null;
	   
	    WordsDatabaseExtractor databaseDataExtractor = null;
	    
	    HashMap<String, String> dataFromTheDatabase = null;
	    
	    File reportFile = null;
	    
	    try{
			configFilePath = PropertiesLoader.getConfigFileFromClasspath(args[0]);
			logger.info("Reading test configuration from classpath file: [" + configFilePath + "]");
			
		    properties = PropertiesLoader.loadProperties(configFilePath);
		    
		    driver = properties.getProperty("maya.database.worddistinct.driver");
		    connectionUrl = properties.getProperty("maya.database.worddistinct.url");
		    username = properties.getProperty("maya.database.worddistinct.username");
		    password = properties.getProperty("maya.database.worddistinct.password");
		    query = properties.getProperty("maya.database.worddistinct.query");
		    queryTimeoutSeconds = Integer.parseInt(properties.getProperty("maya.database.worddistinct.querytimeout"));
		    enableStopwords = Boolean.parseBoolean(properties.getProperty("maya.worddistinct.enablestopwords"));
		    stopWordsFilePath = properties.getProperty("maya.worddistinct.stopwordsfile");
		    enableSynonyms = Boolean.parseBoolean(properties.getProperty("maya.worddistinct.enablesynonyms"));
		    synonymsFilePath = properties.getProperty("maya.worddistinct.synonymsfile");
		    saveReportFileName = properties.getProperty("maya.worddistinct.reportfile");
		    
		    reportFile = new File(saveReportFileName);
		    
		    // ###############################################################
		    
		    File synonymsFile = new File(synonymsFilePath);
		    
		    WordsSynonymsFilter wordsSynonymsFilter = new WordsSynonymsFilter(synonymsFile);
		    
		    wordsSynonymsFilter.process();
		    
		    HashMap<String, Word> loadedSynonyms = wordsSynonymsFilter.getSynonyms();
		    
		    databaseDataExtractor = new WordsDatabaseExtractor(driver, 
		    												   connectionUrl, 
		    												   username, 
		    												   password, 
		    												   query, 
		    												   queryTimeoutSeconds);
		    
		    databaseDataExtractor.connect();
		    
		    if(enableSynonyms){
		       dataFromTheDatabase = databaseDataExtractor.getRows(loadedSynonyms);
		    }else{
		    	  dataFromTheDatabase = databaseDataExtractor.getRows(null);
		    }
		    // ###############################################################

		    WordsDistinctExtractor wordsDistinctExtractor = new WordsDistinctExtractor(dataFromTheDatabase);
		    
		    HashMap<String, Word> wordsDistinct = wordsDistinctExtractor.process();
		    
		    // ###############################################################
		    //TODO Move this into database reader
		    if(enableStopwords){
		       File stopWordsFile = new File(stopWordsFilePath);
		    
		       WordsStopWordsFilter stopWordsFilter = new WordsStopWordsFilter(stopWordsFile);
		    
		       stopWordsFilter.process();
		       
		       HashMap<String, Word> stopWordList = stopWordsFilter.getStopWords();
		    }
		    // ###############################################################
		    
		    WordsSorting wordsSorting = new WordsSorting(wordsDistinct);
		    
		    List<Word> sortedWords = wordsSorting.process();
		    
		    // ###############################################################
		    
		    WordsProcessingReportWriter reportWriter = new WordsProcessingReportWriter(sortedWords, reportFile);
		    
		    reportWriter.write();
		    
		    // ###############################################################
		    
	   }catch(Exception exc){
		      logger.error("Error processing data:", exc);
	   }finally{
	   	        try{
					databaseDataExtractor.disconnect();
				}catch(Exception exc) {
					   logger.error("Unable to close connection");
				}
	   }

	}
}
